package com.chen.mongodb;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author chenyulei
 */
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;

    @GetMapping
    public List<Book> getAllBooks(){
        return bookService.findAll();
    }
    @GetMapping("/{bookId}")
    public Book getByBookId(@PathVariable Integer bookId) {
        return bookService.findById(bookId);
    }

    @PostMapping("/addNewBook")
    @ResponseBody
    public Book addNewBook(@RequestBody Book book) {
        return bookService.save(book);
    }

    @DeleteMapping("/{bookId}")
    public String delete(@PathVariable Integer bookId) {
        Book book = new Book();
        book.setId(bookId);
        bookService.deleteById(bookId);
        return "deleted: " + bookId;
    }
}
